'use strict';

const await = require('await-stream-ready/lib/await');
const Controller = require('./base');

class ManageController extends Controller {
    async status() {
        const { ctx } = this;
        this.success(await ctx.service.manage.status());
    }
    async page() {
        const { ctx } = this;
        const query = ctx.query;
        ctx.validate({
            limit: { type: 'string', required: true },
            page: { type: 'string', required: true },
        }, query);
        this.success(await ctx.service.manage.page(query));
    }
    async create() {
        const { ctx } = this;
        const body = ctx.request.body;
    
        ctx.validate({
        //   username: { type: 'string', required: true },
        //   name: { type: 'string', required: true },
        //   roles: { type: 'array', required: true },
        }, body);
    
        const res = await ctx.service.manage.create(body);
        if (res.success) {
          this.success('添加成功');
        } else {
          this.error('添加失败 ' + (res.msg || ''));
        }
      }
    
    async update() {
        const { ctx } = this;
        const body = ctx.request.body;

        ctx.validate({
            id: { type: 'integer', required: true },
        }, body);

        const res = await ctx.service.manage.update(body);
        if (res.success) {
            this.success('修改成功');
        } else {
            this.error('修改失败 ' + (res.msg || ''));
        }
    }

    async delete() {
        const { ctx } = this;
        const body = ctx.request.body;

        ctx.validate({
            id: { type: 'integer', required: true },
        }, body);
        const success = await ctx.service.manage.delete(body);
        if (success) {
            this.success('删除成功');
        } else {
            this.error('删除失败');
        }
    }
    async query() {
        const { ctx } = this;
        const query = ctx.query;
        this.success(await ctx.service.manage.query(query))
    }
}
module.exports = ManageController;